/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cpproject1;

import java.util.Random;

/**
 *
 * @author Samrat Bhattacharya
 */
public class Player implements IPlayer {
    private IMaze maze;
    /**
     * 
     */
    public MazeCell playerLocation;
    private volatile Boolean isRunning;
    private final int playerRestTime = 500; //msec

    /**
     * 
     * @param maze
     * @param playerLocation
     */
    public Player(IMaze maze, MazeCell playerLocation) {
        this.maze = maze;
        this.playerLocation = playerLocation;
        this.isRunning = true;
    }

    @Override
    public void run() {
        long lastJumpTime = 0;
        Random random = new Random();
        do {
            try {
                //generate Random Direction
                int i = random.nextInt(4);
                Directions tempDirection = Directions.Down;     //for temp purpose
                switch (i)
                {
                    case 0: tempDirection = Directions.Left;
                        break;
                    case 1: tempDirection = Directions.Down;
                        break;
                    case 2: tempDirection = Directions.Right;
                        break;
                    case 3: tempDirection = Directions.Up;
                        break;
                }
                //System.out.println ("Player "+this.playerLocation.x+","+this.playerLocation.y+" moving to "
                if (!maze.Move(tempDirection, this)) //assuming if move succeeds it will not call movejump
                {
                    if ((lastJumpTime == 0
                            || System.currentTimeMillis() - lastJumpTime
                            > this.playerRestTime * 2)
                            && maze.MoveJump(tempDirection, this)) {
                        lastJumpTime = System.currentTimeMillis();
                        //System.out.println ("jump");
                    }
                }
                Thread.sleep(this.playerRestTime);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } while (this.isRunning);
    }

    /**
     * 
     */
    @Override
    public void stop() {
        this.isRunning = false;
    }

    //to be used if player has some initialization routine to be run
    /**
     * 
     */
    @Override
    public void start() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}